const util = require('../../utils/util')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        fabu: '/icon/fabu2.png',
        book: '/icon/rim_book.png',
        weizhi_tu: '/icon/find_weizhi.png',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            _id: options._id
        })
        // console.log('111',this.data._id)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    // 获取发布的内容
    getInput(event) {
        console.log("输入的内容", event.detail.value)
        this.setData({
            pinglunCon: event.detail.value
        })
    },

    //点击发布按钮，增加评论(头像，昵称，日期，内容)至数据库   name:景点名字
    fabu: function () {
        const that = this
        const db = wx.cloud.database();
        const _ = db.command
        this.setData({
            value:''
        })
        wx.cloud.database().collection('tiezi').doc(that.data._id).update({
            data: {
                pinglun: _.unshift({
                    touxiang: getApp().data.userImg,
                    nicheng: getApp().data.userName,
                    date: util.formatDate(new Date()),
                    con: this.data.pinglunCon
                })
            }
        })
            .then(res => {
                console.log('222', res)
                // 将更新后该帖子的评论赋给pinglunList
                wx.cloud.database().collection('tiezi').doc(that.data._id).get()
                    .then(res => {   //请求成功
                        console.log('pinglun', res.data.pinglun)
                        that.setData({
                            pinglunList: res.data.pinglun
                        })
                        console.log('2323', res.data.pinglun)
                    })
                    .catch(err => {  //请求失败
                        console.log('返回评论失败', res.message)
                    })
            }).catch(res => console.log('err'))
    },

    like: function (e) {
        var _id = this.data._id;

        // 获取帖子的count与collect（收藏与否）
        wx.cloud.database().collection('tiezi').doc(_id).get({
            success: res => {
                const data = res.data
                console.log('data', data)
                console.log(data.isDianzan);
                if (data.isDianzan == 'false') {
                    console.log('xihuan')
                    // 改变collect属性
                    wx.cloud.database().collection('tiezi').doc(_id).update({
                        data: {
                            isDianzan: 'true',
                            dianzan: '/icon/yidianzan.png',
                            dianzaned: '已点赞'
                        },
                        success: res => {
                            console.log(data);
                            console.log('collect2');
                            wx.cloud.database().collection('tiezi').doc(_id).get({
                                success: res => {
                                    console.log(res.data);
                                    this.setData({
                                        touxiang: res.data.touxiang,
                                        nicheng: res.data.nicheng,
                                        city: res.data.city,
                                        date: res.data.date,
                                        reci: res.data.reci,
                                        desc: res.data.desc,
                                        images: res.data.fileIDs,
                                        isDianzan: res.data.isDianzan,
                                        isCollect: res.data.isCollect,
                                        dianzan: res.data.dianzan,
                                        collect: res.data.collect,
                                        dianzaned: res.data.dianzaned,
                                        collected: res.data.collected
                                    })
                                    console.log(this.data);
                                },
                                fail(res) {
                                    console.log("请求失败", res)
                                }
                            })
                        },
                        fail: res => {
                            console.log('shibai1')
                        }
                    })
                    //增加数据到我的喜欢里
                    wx.cloud.database().collection('mylike').add({
                        data: {
                            touxiang: data.touxiang,
                            nicheng: data.nicheng,
                            content: data.desc,
                            images: data.fileIDs,
                            createTime: data.createTime,
                            date: data.date,
                            reci: data.reci,
                            id: data._id,
                            isDianzan: 'true'
                        },
                        success: res => {
                            console.log('add after')
                        },
                        fail: res => {
                            console.log('shibai')
                        }
                    })
                    //取消喜欢 
                } else if (data.isDianzan == 'true') {
                    console.log('quxiaoxihuan')
                    // 改变collect属性
                    wx.cloud.database().collection('tiezi').doc(_id).update({
                        data: {
                            isDianzan: 'false',
                            dianzan: '/icon/dianzan.png',
                            dianzaned: '点赞'
                        },
                        success: res => {
                            console.log('quxiaochenggong');
                            wx.cloud.database().collection('tiezi').doc(_id).get({
                                success: res => {
                                    console.log(res.data);
                                    this.setData({
                                        touxiang: res.data.touxiang,
                                        nicheng: res.data.nicheng,
                                        city: res.data.city,
                                        date: res.data.date,
                                        reci: res.data.reci,
                                        desc: res.data.desc,
                                        images: res.data.fileIDs,
                                        isDianzan: res.data.isDianzan,
                                        isCollect: res.data.isCollect,
                                        dianzan: res.data.dianzan,
                                        collect: res.data.collect,
                                        dianzaned: res.data.dianzaned,
                                        collected: res.data.collected
                                    })
                                    console.log(this.data);
                                },
                                fail(res) {
                                    console.log("请求失败", res)
                                }
                            })
                        },
                        fail: res => {
                            console.log('quxiaoshibai')
                        }
                    })
                    // 从我的喜欢列表移除
                    wx.cloud.database().collection('mylike').where(_id).remove({
                        success: res => {
                            console.log('yichuchenggong')
                        },
                        fail: res => {
                            console.log('yichushibai')
                        }
                    })
                } else {
                    console.log(data.isDianzan)
                }

            },
            fail: res => {
                console.log(res.message)
            }
        })
    },

    shoucang: function (e) {
        var _id = this.data._id;

        // 获取帖子的count与collect（收藏与否）
        wx.cloud.database().collection('tiezi').doc(_id).get({
            success: res => {
                const data = res.data;
                console.log('data', data)
                console.log(data.isCollect);
                if (data.isCollect == 'false') {
                    console.log('shoucang')
                    // 改变collect属性
                    wx.cloud.database().collection('tiezi').doc(_id).update({
                        data: {
                            isCollect: 'true',
                            collect: '/icon/yishoucang.png',
                            collected: '已收藏'
                        },
                        success: res => {
                            console.log(data);
                            console.log('collect2');
                            wx.cloud.database().collection('tiezi').doc(_id).get({
                                success: res => {
                                    console.log(res.data);
                                    this.setData({
                                        touxiang: res.data.touxiang,
                                        nicheng: res.data.nicheng,
                                        city: res.data.city,
                                        date: res.data.date,
                                        reci: res.data.reci,
                                        desc: res.data.desc,
                                        images: res.data.fileIDs,
                                        isDianzan: res.data.isDianzan,
                                        isCollect: res.data.isCollect,
                                        dianzan: res.data.dianzan,
                                        collect: res.data.collect,
                                        dianzaned: res.data.dianzaned,
                                        collected: res.data.collected
                                    })
                                    console.log(this.data);
                                },
                                fail(res) {
                                    console.log("请求失败", res)
                                }
                            })
                        },
                        fail: res => {
                            console.log('shibai1')
                        }
                    })
                    //增加数据到我的喜收藏里
                    wx.cloud.database().collection('mycollect').add({
                        data: {
                            touxiang: item.touxiang,
                            nicheng: item.nicheng,
                            content: item.desc,
                            images: item.fileIDs,
                            createTime: item.createTime,
                            date: item.date,
                            reci: item.reci,
                            id: item._id,
                            isCollect: 'true'
                        },
                        success: res => {
                            console.log(item);
                            console.log('add after')
                        },
                        fail: res => {
                            console.log('shibai')
                        }
                    })
                    //取消收藏 
                } else if (data.isCollect == 'true') {
                    console.log('quxiaoxihuan')
                    // 改变collect属性
                    wx.cloud.database().collection('tiezi').doc(_id).update({
                        data: {
                            isCollect: 'false',
                            collect: '/icon/shoucang_2.png',
                            collected: '收藏'
                        },
                        success: res => {
                            console.log('quxiaochenggong');
                            wx.cloud.database().collection('tiezi').doc(_id).get({
                                success: res => {
                                    console.log(res.data);
                                    this.setData({
                                        touxiang: res.data.touxiang,
                                        nicheng: res.data.nicheng,
                                        city: res.data.city,
                                        date: res.data.date,
                                        reci: res.data.reci,
                                        desc: res.data.desc,
                                        images: res.data.fileIDs,
                                        isDianzan: res.data.isDianzan,
                                        isCollect: res.data.isCollect,
                                        dianzan: res.data.dianzan,
                                        collect: res.data.collect,
                                        dianzaned: res.data.dianzaned,
                                        collected: res.data.collected
                                    })
                                    console.log(this.data);
                                },
                                fail(res) {
                                    console.log("请求失败", res)
                                }
                            })
                        },
                        fail: res => {
                            console.log('quxiaoshibai')
                        }
                    })
                    // 从我的收藏列表移除
                    wx.cloud.database().collection('mycollect').doc(_id).remove({
                        success: res => {
                            console.log('yichuchenggong')
                        },
                        fail: res => {
                            console.log('yichushibai')
                        }
                    })
                } else {
                    console.log(data.isCollect)
                }
            },
            fail: res => {
                console.log(res.message)
            }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        const that = this
        // 将该帖子的评论赋给pinglunList
        wx.cloud.database().collection('tiezi').doc(that.data._id).get()
            .then(res => {   //请求成功
                console.log('pinglun', res.data.pinglun)
                that.setData({
                    pinglunList: res.data.pinglun
                })
                console.log('2323', res.data.pinglun,that.data.pinglun==undefined)
            })
            .catch(err => {  //请求失败
                console.log('返回评论失败', res.message)
            })

        // 请求对应卡片的详情
        var _id = this.data._id;
        var city = getApp().data.localCity
        console.log(city)
        console.log(_id)
        wx.cloud.database().collection('tiezi').doc(_id).get({
            success: res => {
                console.log(res.data);
                this.setData({
                    touxiang: res.data.touxiang,
                    nicheng: res.data.nicheng,
                    city: res.data.city,
                    date: res.data.date,
                    reci: res.data.reci,
                    desc: res.data.desc,
                    images: res.data.fileIDs,
                    isDianzan: res.data.isDianzan,
                    isCollect: res.data.isCollect,
                    dianzan: res.data.dianzan,
                    collect: res.data.collect,
                    dianzaned: res.data.dianzaned,
                    collected: res.data.collected
                })
                console.log(this.data);
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    // onShow: function () {
    //     // 请求对应卡片的详情
    //     var _id = this.data._id;
    //     var city = getApp().data.localCity
    //     console.log(city)
    //     console.log(_id)
    //     wx.cloud.database().collection('tiezi').doc(_id).get({
    //         success:res=>{
    //             console.log(res.data);
    //             this.setData({
    //                 touxiang:res.data.touxiang,
    //                 nicheng:res.data.nicheng,
    //                 city:res.data.city,
    //                 date:res.data.date,
    //                 reci:res.data.reci,
    //                 desc:res.data.desc,
    //                 images:res.data.fileIDs,
    //                 isDianzan:res.data.isDianzan,
    //                 isCollect:res.data.isCollect,
    //                 dianzan:res.data.dianzan,
    //                 collect:res.data.collect,
    //                 dianzaned:res.data.dianzaned,
    //                 collected:res.data.collected
    //             })
    //             console.log(this.data);
    //         }
    //     })
    // },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})